package cn.readjava.controller.user;

import cn.readjava.bean.User;
import cn.readjava.constant.ShiroConstant;
import cn.readjava.service.UserService;
import cn.readjava.util.ResponseResult;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/info")
    public String info() {
        return "user/user-info";
    }

    // 基本信息更新
    @PostMapping("/info")
    public String info(User user, HttpServletRequest request, Model model) {
        User replyName = userService.findByUserName(user.getUserName());
        if (replyName != null && replyName.getUserId() != user.getUserId()) {
            model.addAttribute("result", new ResponseResult(100, "更新失败，该用户名已经被使用"));
            return "user/user-info";
        }
        User dbUser = userService.getOne(user.getUserId());
        dbUser.setUserName(user.getUserName());
        User save = userService.save(dbUser);
        if (save != null) {
            request.getSession().setAttribute("currentUser", save);
            model.addAttribute("result", new ResponseResult(200, "更新成功"));
            return "user/user-info";
        }
        model.addAttribute("result", new ResponseResult(100, "更新失败，请检查数据是否合法"));
        return "user/user-info";
    }

    @GetMapping("/password")
    public String password() {
        return "user/user-password";
    }


    // 修改密码
    @PostMapping("/password")
    public String password(@RequestParam(value = "userId") Long userId,
                           @RequestParam(value = "oldPassword") String oldPassword,
                           @RequestParam(value = "newPassword") String newPassword,
                           @RequestParam(value = "newPasswordCheck") String newPasswordCheck,
                           Model model) {
        if (!newPassword.equals(newPasswordCheck)) {
            model.addAttribute("result", new ResponseResult(100, "更新失败，两次输入密码不一致"));
            return "user/user-password";
        }
        User user = userService.getOne(userId);
        Md5Hash md5Pwd = new Md5Hash(oldPassword, user.getUserEmail() + ShiroConstant.SALT, ShiroConstant.HASHITERATIONS);
        if (!md5Pwd.toString().equals(user.getUserPassword())) {
            model.addAttribute("result", new ResponseResult(100, "更新失败，原密码错误"));
            return "user/user-password";
        }
        Md5Hash newPwd = new Md5Hash(newPassword, user.getUserEmail(), 1024);
        user.setUserPassword(newPwd.toString());
        User save = userService.save(user);
        if (save != null) {
            return "redirect:/logout";
        }
        model.addAttribute("result", new ResponseResult(100, "更新失败，请检查数据是否合法"));
        return "user/user-password";
    }
}
